package com.keshan.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.keshan.pojo.*;
import com.keshan.service.SleepServices;
import com.keshan.service.UserDeviceServices;
import com.keshan.utils.DateUtil;
import com.keshan.utils.JWTUtils;
import com.keshan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @author keshan
 * @date 2022/10/21 - 19:41
 */
@CrossOrigin
@Controller
@RequestMapping("/sleep")
public class SleepController {
    @Autowired
    SleepServices sleepServices;
    @Autowired
    UserDeviceServices userDeviceServices;

    @RequestMapping("/insertsleep")
    @ResponseBody
    public Msg insertBySelectiveToSleep(@RequestBody SleepJson sleepJson) {
        Date date = new Date();
        System.out.println(sleepJson.toString());
        System.out.println(date.toString());
        List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByImei(sleepJson.getImei());
        try {
            Sleep sleep = new Sleep();
            sleep.setAwakeTime(Double.parseDouble(sleepJson.getAwake_time()));
            sleep.setDeepSleep(Double.parseDouble(sleepJson.getDeep_sleep()));
            sleep.setLightSleep(Double.parseDouble(sleepJson.getLight_sleep()));
            sleep.setTotalSleep(Double.parseDouble(sleepJson.getTotal_sleep()));
            sleep.setDeviceType(sleepJson.getDevice_type());
            sleep.setUsername(userDeviceList.get(0).getUsername());
            sleep.setTimeBegin(DateUtil.StringToDate(sleepJson.getTime_begin_str()));
            sleep.setTimeEnd(DateUtil.StringToDate(sleepJson.getTime_end_str()));
            System.out.println(sleep.toString());
            int result = sleepServices.insertBySelectiveSleep(sleep);
            return Msg.success().add("result", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Msg.failInsert();
    }

    @RequestMapping("/sleeplisthidetoday")
    @ResponseBody
    public Msg selectSleepListHideByImeiToday(HttpServletRequest httpServletRequest) {

        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            List<SleepHide> sleepList = sleepServices
                    .getSleepListHideByUsernameToday(username);
            return Msg.success().add("sleepList", sleepList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");

    }

    @RequestMapping("/sleeplisteveryday")
    @ResponseBody
    public Msg selectSleepEverydayDataListByImei(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            List<SleepEverydayData> sleepEverydayDataList = sleepServices
                    .getSleepEverydayDataList(username);
            return Msg.success().add("sleepDataList", sleepEverydayDataList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }
}
